﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace ModwindDomainInfo
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            AboutBox about = new AboutBox();
            about.ShowDialog();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            Dictionary<string, Domain> domains = new Dictionary<string, Domain>();
            Dictionary<string, IsoCountry> isoCountries = new Dictionary<string, IsoCountry>();
            Dictionary<string, GostCountry> gostCountries = new Dictionary<string, GostCountry>();

            // Read XML
            {
                string xml = Properties.Resources.data;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xml);
                foreach (XmlNode root in xmlDoc.ChildNodes)
                {
                    if (root.Name != "root") continue;
                    foreach (XmlNode group in root.ChildNodes)
                    {
                        switch (group.Name)
                        {
                            case "domains":
                                foreach (XmlNode item in group.ChildNodes)
                                {
                                    if (item.Name != "domain") continue;
                                    Domain it = new Domain();
                                    foreach (XmlNode field in item.ChildNodes)
                                    {
                                        switch (field.Name)
                                        {
                                            case "code":
                                                it.code = field.InnerText;
                                                break;
                                            case "type":
                                                it.type = field.InnerText;
                                                break;
                                            case "sponsor":
                                                it.sponsor = field.InnerText;
                                                break;
                                        }
                                    }
                                    domains[it.code] = it;
                                }
                                break;
                            case "iso":
                                foreach (XmlNode item in group.ChildNodes)
                                {
                                    if (item.Name != "country") continue;
                                    IsoCountry it = new IsoCountry();
                                    foreach (XmlNode field in item.ChildNodes)
                                    {
                                        switch (field.Name)
                                        {
                                            case "code":
                                                it.code = field.InnerText;
                                                break;
                                            case "name":
                                                it.name = field.InnerText;
                                                break;
                                            case "status":
                                                it.status = field.InnerText;
                                                break;
                                        }
                                    }
                                    isoCountries[it.code] = it;
                                }
                                break;
                            case "gost":
                                foreach (XmlNode item in group.ChildNodes)
                                {
                                    if (item.Name != "country") continue;
                                    GostCountry it = new GostCountry();
                                    foreach (XmlNode field in item.ChildNodes)
                                    {
                                        switch (field.Name)
                                        {
                                            case "name":
                                                it.name = field.InnerText;
                                                break;
                                            case "ru3":
                                                it.ru3 = field.InnerText;
                                                break;
                                            case "en3":
                                                it.en3 = field.InnerText;
                                                break;
                                            case "en2":
                                                it.en2 = field.InnerText;
                                                break;
                                        }
                                    }
                                    gostCountries[it.en2] = it;
                                }
                                break;
                        }
                    }
                }
            }

            
            foreach (KeyValuePair<string, Domain> pair in domains)
            {
                Domain domain = pair.Value;
                DomainInfo info=new DomainInfo();
                info.domainName = domain.code;
                info.countryCode = info.domainName.Replace(".","");
                info.domainType = domain.type;
                info.domainSponsor = domain.sponsor;

                try
                {
                    IsoCountry country = isoCountries[info.countryCode];
                    info.countryNameEn = country.name;
                    info.countryCodeStatus = country.status;
                }
                catch { }


                try
                {
                    GostCountry country = gostCountries[info.countryCode];
                    info.countryNameRu = country.name;
                }
                catch { }

                infos[info.domainName] = info;
            }

            foreach (KeyValuePair<string, DomainInfo> pair in infos)
            {
                lstCountry.Items.Add(pair.Key);
            }
        }
        Dictionary<string, DomainInfo> infos = new Dictionary<string, DomainInfo>();

        private void lstCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstCountry.SelectedItem == null)
            {
                return;
            }

            string domainName = lstCountry.SelectedItem.ToString();
            try
            {
                DomainInfo info = infos[domainName];
   
                string imageName = domainName.Substring(1);
                picFlag.Image = Properties.Resources.ResourceManager.GetObject(imageName) as Image;

                string countryName = info.countryNameEn;

                string description ="";
                if (info.domainType != null)
                {
                    description += "Type: "+info.domainType + "\r\n\r\n";
                }

                if (countryName != null)
                {
                    description += "Country: " + countryName;
                    if (info.countryCodeStatus != null) description+= " ("+info.countryCodeStatus+") ";
                    description += "\r\n\r\n";
                }

                if (info.domainSponsor != null)
                {
                    description += "Sponsor: " + info.domainSponsor;
                }

                if (picFlag.Image == null) picFlag.Image = Properties.Resources.none;
                if (countryName == null) countryName = "N / A";
                lblCountry.Text = countryName;
                boxDesc.Text = description;
            }
            catch { }
        }
    }

    public class Domain
    {
        public string code;
        public string type;
        public string sponsor;
    };

    public class IsoCountry
    {
        public string code;
        public string name;
        public string status;
    };

    public class GostCountry
    {
        public string name;
        public string ru3;
        public string en3;
        public string en2;
    };

    public class DomainInfo
    {
        public string domainName;
        public string domainType;
        public string domainSponsor;

        public string countryCode;
        public string countryCodeStatus;

        public string countryNameEn;
        public string countryNameRu;
    }
}
